if(typeof(mstar)=="undefined"){
	var mstar = {};
}
if(typeof(QS_Theme)=="undefined"){
	QS_Theme="default";
}
if(typeof(QS_ImgServer)=="undefined"){
    QS_ImgServer = "//rtqimg.morningstar.com/";
}

mstar.draw = function(sid,wid){
	this.workspaceId = sid;
	this.windowId = wid;
	this.series = null;
	this.drawing={
		type:{
			trendline:false,
			rectangle:false,
			ellipse:false,
			note:false,
			levelline:false,
			fibonacci:false
		},
		abbDom:null,//abbreviative help text
		start:"",
		operationType:"",
		trendline:{
			func:{
				movingDraw:true,
				dbClickDeleteLine:true,
				moveLine:false,
				clickRedraw:true,
                dragReDraw:true
			},
			abbDom:$("<div class='rtc-dr rtc-dr-trendline'></div>"),
			style:{
				weight:1,
				color:"#65A125",
				lineWrapColor:"#fff",
				lineWrapWidth:1
			},
			depot:[],
            move:{
                moving:false,
                cp:[],
                sp:[],
                ep:[],
                ix:0
            },
			currentIndex:0,
			count:0,
			end:true
		},
		rectangle:{
			func:{
				movingDraw:true,
				clickRedraw:true,
				dragReDraw:true
			},
			style:{
				border:{
					width:1,
					color:"#65A125"
				},
				fill:{
					show:false,
					opacity:0.3,
					color:"#fffef9"
				}
			},
			abbDom:$("<div class='rtc-dr rtc-dr-rectangle'></div>"),
			count:0,
			depot:[],
			move:{
				moving:false,
				cp:[],
				sp:[],
				ep:[],
				ix:0
			}
		},
		ellipse:{
			func:{
				movingDraw:true,
				clickRedraw:true,
				dragReDraw:true
			},
			style:{
				border:{
					width:1,
					color:"#65A125"//d71345
				},
				fill:{
					show:true,
					opacity:0.3,
					color:"#fffef9"
				}
			},
			abbDom:$("<div class='rtc-dr rtc-dr-ellipse'></div>"),
			count:0,
			depot:[],
			move:{
				moving:false,
				cp:[],
				sp:[],
				ep:[],
				ix:0
			}
		},
		levelline:{
			func:{
				dbClickDeleteLine:true,
				movingDraw:true,
				dragReDraw:true
			},
			style:{
				lineWidth:2,
				lineColor:"#65A125",
				lineWrapWidth:8,
				lineWrapColor:"#fff"
			},
			abbDom:$("<div class='rtc-dr rtc-dr-ellipse'></div>"),
			count:0,
			depot:[],
			move:{
				moving:false,
				cp:[],
				sp:[],
				ep:[],
				ix:0
			}
		},
		note:{
			func:{
				moving:true
			},
			depot:[],
			currentIndex:0,
			count:0,
			abbDom:$("<span class='qs-flot-abb-ellipse-hint'>NOTE</span>")
		},
		fibonacci:{
			func:{
				movingDraw:true,
				moveLine:false,
				clickRedraw:true
			},
			abbDom:$("<div class='rtc-dr rtc-dr-fibonacci'></div>"),
			style:{
				"100":{
					weight:2,
					color:"#ED0D0D"
					},
				"61.8":{
					weight:1,
					color:"#DB5858"
				},
				"50":{
					weight:1,
					color:"#DB5858"
				},
				"38.2":{
					weight:1,
					color:"#DB5858"
				},
				"0":{
					weight:2,
					color:"#ED0D0D"
				},
				"sepoint":{
					weight:1,
					color:"#ED0D0D"
				},
				weight:1,
				color:"#627BA4"
			},
			depot:[],
			end:true,
			count:0,
			move:{
				moving:false,
				cp:[],
				sp:[],
				ep:[],
				ix:0
			}
		},
		count:0,
		startend:""
	};
	
};
mstar.draw.prototype={
	getStart:function(){
		return this.drawing.start;
	},
	setDrawType:function(t,f){
		this.drawing.type[t]=f;
	},
	getDrawTypes:function(){
		return this.drawing.type;
	},
	drawGraph:function(ct,axes,plot,width,align,ost){
		this._drawingTrendLine(ct,axes,plot);
		this._drawRectangle(ct,axes);
		this._drawEllipse(ct,axes);
		this._drawingLevelline(ct,axes,plot,width,align,ost);
		this._drawingFibonacci(ct,axes,plot,width,align,ost);
	},
	saveNotes:function(series){
//		var nt = this.drawing.note,t = null,x =0;y=0,arr=[];
//		for(var i = 0;i<nt.depot.length;i++){
//			t =nt.depot[i];
//			arr = series.data[t.x];
//			x = RTQ_ChartUtil.toDateFromIndex(arr[arr.length-1]);
//			y = t.y;
//			//console.log(t.note.saveNote(x,y));
//		}
	},
	renderNote:function(series,data,target){
		var t = null,map = {},j = 0,time=null,dlt=null;
		for(var i = 0;i<series.data.length-1;i++){
			t = series.data[i],tn=series.data[i+1];
			if(t[t.length-1]>100000){
				time1 = t[t.length-1];
				time2 =tn[tn.length-1];
			}else{
				time1 = RTQ_ChartUtil.toDateFromIndex(t[t.length-1]).getTime();
				time2 = RTQ_ChartUtil.toDateFromIndex(tn[tn.length-1]).getTime();
			}
			for(j=0;j<data.list.length;j++){
				dlt =data.list[j].eventTime;
				if(dlt>=time1&&dlt<time2){
					if(typeof(map[i])=="undefined"){
						map[i]=[];
					}
					map[i].push(data.list[j]);
				}
				else if(dlt>time2){
					break;
				}
			}
		}
		for (var i in map) {
			var count = 0;
			for (var j = 0; j < map[i].length; j++) {
				var x = i,y =parseFloat(map[i][j].eventInfo.y);
				var tmap = map[i][j];
				var dw = this.drawing,nt = dw.note;
                var sdata = series;
                if (typeof(series.otherInfo) == "undefined") {
                    sdata = series[0];
                }
				var note = new mstar.note(target,sdata,new mstar.noteDataManager($.extend(true,{secId:sdata.otherInfo.ticker},tmap)));
				note.setPosition(sdata.xaxis.p2c(x),count*12);
				note.currentIndex = nt.depot.length;
				note.dom.attr("idx",nt.depot.length);
				tmap.eventInfo =map[i][j].eventInfo==""?{"text":"","status":"","h":"","w":""}:$.evalJSON(map[i][j].eventInfo);
				tmap.eventInfo.status="close";
				note.restoreNote(tmap.eventInfo);
				note.nx = i;
				var current = {note:note,dataIndex:1,x:x,y:y};	
				nt.depot.push(current);
				nt.count ++;
				count++;
			}
		}
	},
	_drawingFibonacci:function(ct,axes,plot){
		if(this.drawing.fibonacci.depot.length==0||this.drawing.fibonacci.count<=0){
			return;
		}
		ct.save();
		ct.beginPath();
		var fi = this.drawing.fibonacci,t;
		var x1=0,y1=0,x2=0,y2=0,height=0,diff=0;
		for(var i = 0;i<fi.depot.length;i++){
			t = fi.depot[i];
			if(t ==undefined){
				continue;
			}
			x1=axes.xaxis.p2c(t.start.x),y1 = axes.yaxis.p2c(t.start.y);
			x2=axes.xaxis.p2c(t.end.x);y2=axes.yaxis.p2c(t.end.y);
			height = y2-y1;
			diff = t.end.y-t.start.y;
			var minx=maxx=0;
			if(t.end.x!=undefined&&t.end.y!=undefined){
				ct.beginPath();
				ct.fillStyle = fi.style["sepoint"].color;
				ct.lineWidth = fi.style["sepoint"].weight;
				ct.arc(x1,y1, 3, 0, 2 * Math.PI, true);
				ct.arc(x2,y2, 3, 0, 2 * Math.PI, true);
				ct.fill();
				ct.beginPath();
				ct.strokeStyle = fi.style.color;
				ct.lineWidth = fi.style.weight;
				ct.moveTo(x1,y1);
				ct.lineTo(x2,y2);
				ct.stroke();
				ct.beginPath();
				ct.strokeStyle = fi.style["0"].color;
				ct.lineWidth = fi.style["0"].weight;
				ct.moveTo(x1,y2);
				ct.lineTo(x2,y2);
				ct.stroke();
				ct.beginPath();
				ct.strokeStyle = fi.style["100"].color;
				ct.lineWidth = fi.style["100"].weight;
				ct.moveTo(x1,y1);
				ct.lineTo(x2,y1);
				ct.stroke();
				ct.beginPath();
				ct.strokeStyle = fi.style["61.8"].color;
				ct.lineWidth = fi.style["61.8"].weight;
				ct.moveTo(x1,y2-height*0.618);
				ct.lineTo(x2,y2-height*0.618);
				
				ct.stroke();
				ct.beginPath();
				ct.strokeStyle = fi.style["50"].color;
				ct.lineWidth = fi.style["50"].weight;
				ct.moveTo(x1,y2-height*0.5);
				ct.lineTo(x2,y2-height*0.5);
				ct.stroke();
				ct.beginPath();
				ct.strokeStyle = fi.style["38.2"].color;
				ct.lineWidth = fi.style["38.2"].weight;
				ct.moveTo(x1,y2-height*0.382);
				ct.lineTo(x2,y2-height*0.382);
				if(t.opDom !=null){
					minx = Math.min(x2,x1)+plot.left,maxx =Math.max(x2,x1)+plot.left;
					t.opDom[0].css({left:maxx-37,top:y2+3});
					t.opDom[9].css({left:minx,top:y2+3});
					t.opDom[0].html(QS_NumFmt.Fmt2(t.end.y,true," ",2));
					if(typeof(t.opDom[10])!="undefined"&&t.opDom[10]!=null){
						t.opDom[10].css({left:maxx+1,top:Math.min(y1,y2)-3});  //make the close btn always on top right - david
					}
					t.opDom[1].css({left:maxx-37,top:y1+3});
					t.opDom[5].css({left:minx,top:y1+3});
					t.opDom[1].html(QS_NumFmt.Fmt2(t.start.y,true," ",2));
					t.opDom[2].css({left:maxx-37,top:y2-height*0.618+3});
					t.opDom[6].css({left:minx,top:y2-height*0.618+3});
					t.opDom[2].html(QS_NumFmt.Fmt2(t.start.y+diff*0.382,true," ",2));
					t.opDom[3].css({left:maxx-37,top:y2-height*0.5+3});
					t.opDom[7].css({left:minx,top:y2-height*0.5+3});
					t.opDom[3].html(QS_NumFmt.Fmt2(t.start.y+diff*0.5,true," ",2));
					t.opDom[4].css({left:maxx-37,top:y2-height*0.382+3});
					t.opDom[8].css({left:minx,top:y2-height*0.382+3});
					t.opDom[4].html(QS_NumFmt.Fmt2(t.start.y+diff*0.618,true," ",2));
				}
			}
			
		}
        ct.stroke();
		ct.restore();
	},
	_drawingTrendLine:function(ct,axes,plot){
		if(this.drawing.trendline.depot.length==0||this.drawing.trendline.count<=0){
			return;
		}
		ct.save();
		ct.beginPath();
		var tl = this.drawing.trendline,t;
		ct.strokeStyle = tl.style.color;
		ct.lineWidth = tl.style.weight;
		for(var i = 0;i<tl.depot.length;i++){
			t = tl.depot[i];
			if(t ==undefined){
				continue;
			}
			if(t.end.x!=undefined&&t.end.y!=undefined){
				ct.moveTo(axes.xaxis.p2c(t.start.x),axes.yaxis.p2c(t.start.y));
				ct.lineTo(axes.xaxis.p2c(t.end.x),axes.yaxis.p2c(t.end.y));
				if(t.opDom !=null){
					t.opDom.css({left:axes.xaxis.p2c(t.end.x)+plot.left+18,top:axes.yaxis.p2c(t.end.y)-plot.top-13});
				}
				
			}
			
		}
        ct.stroke();
		ct.beginPath();
		if(this.drawing.operationType=="drag-trendline"){
            ct.strokeStyle = mstar.flotUtil.colorToRGB(tl.style.lineWrapColor, 0.4);
            ct.lineWidth = tl.style.lineWrapWidth;
            ct.lineCap = "round";
            //var sx =tl.style.lineWrapWidth/2,ex =width-ll.style.lineWrapWidth/2;
            for (var i = 0; i < tl.depot.length; i++) {
                t = tl.depot[i];
                if (t == undefined) {
                    continue;
                }
                if (i == tl.currentIndex && t.end.x != undefined && t.end.y != undefined) {
                    ct.moveTo(axes.xaxis.p2c(t.start.x), axes.yaxis.p2c(t.start.y) + 2);
                    ct.lineTo(axes.xaxis.p2c(t.end.x), axes.yaxis.p2c(t.end.y) + 2);
                    ct.moveTo(axes.xaxis.p2c(t.start.x), axes.yaxis.p2c(t.start.y) - 2);
                    ct.lineTo(axes.xaxis.p2c(t.end.x), axes.yaxis.p2c(t.end.y) - 2);
                    ct.stroke();
                    ct.fillStyle = mstar.flotUtil.colorToRGB(tl.style.lineWrapColor, 0.4);
                    ct.arc(axes.xaxis.p2c(t.end.x), axes.yaxis.p2c(t.end.y) - 1, 2, -Math.PI, Math.PI, false);
                    ct.arc(axes.xaxis.p2c(t.start.x), axes.yaxis.p2c(t.start.y), 2, -Math.PI, Math.PI, false);
                    ct.fill();
                    break;
                }
            }
            ct.stroke();
		}
		ct.restore();
	},
	_getEllipsePoints:function(t,axes){
		var a1=[],a2=[];
        var tpos = this._calculateRectangle(t, axes);
        var aa = tpos.w / 2, ab = tpos.h / 2;
        for (var k = ab; k >= 0; k -= 0.5) {
            a2.push(k);
            var tosq = aa * aa - aa * aa * k * k / (ab * ab);
            if (tosq < 0) {
                tosq = 0;
            }
            a1.push(Math.sqrt(tosq));
        }
        var or = [tpos.x + aa, tpos.y + ab];
		return [a1,a2,or];
	},
	_drawEllipse:function(ct,axes){
		var ell = this.drawing.ellipse,t;
		if(ell.depot.length==0){
			return;
		}
		ct.save();
		ct.beginPath();
		ct.strokeStyle = ell.style.border.color;
		ct.lineWidth = 1;
		var a1=[],a2=[],a3=[],a4=[],or=[];
		for(var i = 0;i<ell.depot.length;i++){
			a1=[],a2=[];
			t = ell.depot[i];
			if(t ==undefined){
				continue;
			}
			if(t.end.x!=undefined&&t.end.y!=undefined){
				var epoints = this._getEllipsePoints(t,axes);
				a1 =epoints[0],a2=epoints[1],or = epoints[2];
				if(isNaN(a1[0])){
					continue;
				}
				ct.moveTo(or[0]+a1[0],or[1]-a2[0]);
				for(var k  =0;k<a1.length;k++){
					ct.lineTo(or[0]+a1[k],or[1]-a2[k]);
				}
				for(var k  =a1.length-1;k>=0;k--){
					ct.lineTo(or[0]+a1[k],or[1]+a2[k]);
				}
				for(var k  =0;k<a1.length;k++){
					ct.lineTo(or[0]-a1[k],or[1]+a2[k]);
				}
				for(var k  =a1.length-1;k>=0;k--){
					ct.lineTo(or[0]-a1[k],or[1]-a2[k]);
				}
			}
		}
        ct.stroke();
		
		var dd = ell.depot[ell.currentIndex];
		if(ell.resizeHandler){
            if (dd && dd.start.x && dd.start.y) {
                ct.beginPath();
                ct.arc(axes.xaxis.p2c(dd.start.x) + 1, axes.yaxis.p2c(dd.start.y) - 1, 2, -Math.PI, Math.PI, false);
                ct.stroke();
            }
            if (dd && dd.end.x && dd.end.y) {
                ct.beginPath();
                ct.arc(axes.xaxis.p2c(dd.end.x) + 1, axes.yaxis.p2c(dd.end.y) - 1, 2, -Math.PI, Math.PI, false);
                ct.stroke();
            }
		}
		if(this.drawing.operationType=="drag-ellipse"&&ell.style.fill.show){
//			ct.globalAlpha =ell.style.fill.opacity;
//			ct.fillStyle = ell.style.fill.color;
			ct.fillStyle = mstar.flotUtil.colorToRGB(ell.style.fill.color, 0.3);
			if(dd.end.x!=undefined&&dd.end.y!=undefined){
				var epoints = this._getEllipsePoints(dd,axes);
				a1 =epoints[0],a2=epoints[1],or = epoints[2];
				if (!isNaN(a1[0])) {
					ct.moveTo(or[0] + a1[0], or[1] - a2[0]);
					for (var k = 0; k < a1.length; k++) {
						ct.lineTo(or[0] + a1[k], or[1] - a2[k]);
					}
					for (var k = a1.length - 1; k >= 0; k--) {
						ct.lineTo(or[0] + a1[k], or[1] + a2[k]);
					}
					for (var k = 0; k < a1.length; k++) {
						ct.lineTo(or[0] - a1[k], or[1] + a2[k]);
					}
					for (var k = a1.length - 1; k >= 0; k--) {
						ct.lineTo(or[0] - a1[k], or[1] - a2[k]);
					}
				}
			}
			ct.fill();
		}
		
		ct.restore();
	},
	_drawRectangle:function(ct,axes){
		var dw = this.drawing,rt = dw.rectangle,t = null,calRect=null;
		ct.save();
		ct.beginPath();
		ct.lineWidth=rt.style.border.width;
		ct.strokeStyle=rt.style.border.color;
		for(var i = 0;i<rt.depot.length;i++){
			t = rt.depot[i];
			if(t !=undefined &&t.start !=undefined &&t.end !=undefined&&t.end.x !=undefined&&t.end.y !=undefined&&t.start.x !=undefined&&t.start.y !=undefined){
				calRect = this._calculateRectangle(t,axes);
				ct.rect(calRect.x,calRect.y,calRect.w,calRect.h);
			}
		}
		ct.stroke();
		ct.beginPath();
		if(dw.operationType=="drag-rectangle"){
			t = rt.depot[rt.currentIndex];
			if (t != undefined && t.start != undefined && t.end != undefined && t.end.x != undefined && t.end.y != undefined && t.start.x != undefined && t.start.y != undefined) {
				calRect = this._calculateRectangle(t,axes);
				ct.fillStyle = mstar.flotUtil.colorToRGB(rt.style.fill.color, 0.3);
				ct.rect(calRect.x,calRect.y,calRect.w,calRect.h);
				ct.fill();
			}
		}
		ct.restore();
	},
	_drawingLevelline:function(ct,axes,plot,width,align,ost){
		if(this.drawing.levelline.depot.length==0||this.drawing.levelline.count<=0){
			return;
		}
		ct.save();
		var ll = this.drawing.levelline,t;
		ct.beginPath();
		ct.strokeStyle = mstar.flotUtil.colorToRGB(ll.style.lineWrapColor,0.5);;
		ct.lineWidth = ll.style.lineWrapWidth;
		ct.lineCap = "round";
		var sx =ll.style.lineWrapWidth/2,ex =width-ll.style.lineWrapWidth/2;
		for(var i = 0;i<ll.depot.length;i++){
			t = ll.depot[i];
			if(t ==undefined){
				continue;
			}
            if (i == ll.currentIndex) {
                if (t.start.x != undefined && t.start.y != undefined) {
                    ct.moveTo(sx, axes.yaxis.p2c(t.start.y));
                    ct.lineTo(ex, axes.yaxis.p2c(t.start.y));
                }
            }
		}
        ct.stroke();
		ct.beginPath();
		ct.strokeStyle = ll.style.lineColor;
		ct.lineWidth = ll.style.lineWidth;
		sx =0,ex =width;
		if(align=="left"){
			//sx = ost.left;
		}
		for(var i = 0;i<ll.depot.length;i++){
			t = ll.depot[i];
			if(t ==undefined){
				continue;
			}
			var flag = true;
			if(t.start.x!=undefined&&t.start.y!=undefined){
				 if (i != ll.currentIndex) {
				 	ct.moveTo(sx,axes.yaxis.p2c(t.start.y));
					ct.lineTo(ex,axes.yaxis.p2c(t.start.y));
					if(axes.yaxis.p2c(t.start.y)<0||axes.yaxis.p2c(t.start.y)>ost){
						flag = false;
						t.opDom.hide();
					}
				 }else{
				 	ct.moveTo(sx+ll.style.lineWrapWidth/2,axes.yaxis.p2c(t.start.y));
					ct.lineTo(ex-ll.style.lineWrapWidth/2,axes.yaxis.p2c(t.start.y));
				 }
				 if(t.opDom!=null&&flag){
				 	t.opDom.show();
						t.opDom.find("span.chart-flot-txt").html(QS_NumFmt.Fmt2(t.start.y,true," ",2));
						t.opDom.css({left:align=="right"?width:0,top:axes.yaxis.p2c(t.start.y)-plot.top-5});
				 }
//				if(t.opDom !=null){
//					t.opDom.css({left:axes.xaxis.p2c(t.end.x)+plot.left+18,top:axes.yaxis.p2c(t.end.y)-plot.top-13});
//				}
			}
			
		}
        ct.stroke();
		ct.restore();
	},
	_storeCurrent:function(item,y,opdom,obj){
		var current = {start:{x:item.datapoint[0],y:item.series.yaxis.c2p(y)},end:{},opDom:opdom};
		obj.depot.push(current);
		obj.currentIndex = obj.depot.length-1;
		obj.count ++;
	},
	_storeBeforeMoving:function(obj,current,item,y,target){
        obj.move.moving = true;
        target.css("cursor", "move");
        obj.move.cp = [item.datapoint[0], item.series.yaxis.c2p(y)];
        obj.move.sp = [current.start.x, current.start.y];
        obj.move.ep = [current.end.x, current.end.y];
	},
	mouseDown:function(e,xy,item,axes,target,plot,series){
		this.series = series;
		var canvasX=xy[0],canvasY=xy[1];
		var self = this;
		var dw = this.drawing,tl = dw.trendline,rt = dw.rectangle,ell = dw.ellipse;ll = dw.levelline,nt = dw.note,fi = dw.fibonacci;
		if(item==null){
			return;
		}
		if(dw.type.trendline){
			var dom=$("<div class='changeHintDiv rtq-chart-hover-hint'><div class='wrap-img'><img class='arrow' src='"+QS_ImgServer+(QS_Theme=="black"?"theme/black/arrow.gif":"img/arrow.gif")+"' border='0'/><span class='wrap-text-close'><span class='rtq-trendline-hinttext'>bbb</span> <a href='javascript:void(0)'><img src='"+QS_ImgServer+(QS_Theme=="black"?"theme/black/btn_close.gif":"img/btn_close.gif")+"' border='0'/></a> </span></div></div>");
			dom.appendTo(target);
			dom.hide();
			if(tl.movingDraw){
				dom.show(100);
			}
			var tIndex = tl.depot.length;
			dom.find("a").bind("click.tline",function(){
				setTimeout(function(){self._clearTrendLine(self,tIndex);},50);
			})
			dw.start="trendline";
			this._storeCurrent(item,xy[1],dom,tl);
		}else if(dw.type.rectangle){
			dw.start="rectangle";
			this._storeCurrent(item,xy[1],null,rt);
		}else if (dw.type.ellipse) {
			dw.start="ellipse";
			this._storeCurrent(item,xy[1],null,ell);
		}else if (dw.type.levelline) {
			var tdom = $("<div class='chart-flot-drawing j-chart-flot-drawing-levelline'><span class='chart-flot-txt'></span><span class='chart-flot-colose-span'>X</span></div>");
			target.append(tdom);
			tdom.hover(function(){$(this).find(".chart-flot-colose-span").toggle()});
			var tIndex = ll.depot.length;
			tdom.bind("click.levelline",function(){
				self._clearLevelline(self,tIndex);
			})
			dw.start="levelline";
			this._storeCurrent(item,xy[1],tdom,ll);
		}else if(dw.type.note){
			var didx = item.datapoint[item.datapoint.length-1];
			var sdata = series;
			if(typeof(series.otherInfo)=="undefined"){
				sdata = series[0];
			}
			var ddate = didx<100000?RTQ_ChartUtil.toDateFromIndex(didx).getTime():didx;
			var ds = new mstar.noteDataManager({secId:sdata.otherInfo.ticker,eventTime:ddate});
			var note = new mstar.note(target,sdata,ds );
			note.setPosition(item.series.xaxis.p2c(item.datapoint[0])+plot.left,item.series.yaxis.p2c(item.series.yaxis.c2p(xy[1])));
			note.currentIndex = nt.depot.length;
			note.dom.attr("idx",nt.depot.length);
			
			var current = {note:note,x:item.datapoint[0],y:item.series.yaxis.c2p(xy[1])};	
			nt.depot.push(current);
			nt.currentIndex = nt.depot.length-1;
			dw.start="note";
			nt.count ++;
			note.addNote();
		} else if(dw.type.fibonacci){
			var current = {start:{x:item.datapoint[0],y:item.series.yaxis.c2p(xy[1])},end:{},opDom:null};
			var idx = "chart-fibonacci-index-"+fi.depot.length;
			var str = "<div class='rtq-chart-fibonacci-tip "+idx+"' index='l1'>"+QS_NumFmt.Fmt2(100,true," ",1)+QSLGTXT.percent+"</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='l2'>"+QS_NumFmt.Fmt2(61.8,true," ",1)+QSLGTXT.percent+"</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='l3'>"+QS_NumFmt.Fmt2(50,true," ",1)+QSLGTXT.percent+"</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='l4'>"+QS_NumFmt.Fmt2(38.2,true," ",1)+QSLGTXT.percent+"</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='l5'>"+QS_NumFmt.Fmt2(0,true," ",1)+QSLGTXT.percent+"</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='r1'>1</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='r2'>2</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='r3'>3</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='r4'>4</div>"+
						"<div class='rtq-chart-fibonacci-tip "+idx+"' index='r5'>5</div>";
			var dom=$(str);
			dom.appendTo(target);
			current.opDom =[target.find("."+idx+"[index='r1']"),target.find("."+idx+"[index='r2']"),target.find("."+idx+"[index='r3']"),target.find("."+idx+"[index='r4']"),target.find("."+idx+"[index='r5']"),target.find("."+idx+"[index='l1']"),target.find("."+idx+"[index='l2']"),target.find("."+idx+"[index='l3']"),target.find("."+idx+"[index='l4']"),target.find("."+idx+"[index='l5']")];
			fi.depot.push(current);
			fi.currentIndex = fi.depot.length-1;
			dw.start="fibonacci";
			fi.count ++;
		}else {
			var istl = false;
			if ((tl.func.clickRedraw || tl.func.dragReDraw) && tl.count > 0) {
				var td = null;
				var flag = false;
				for (var i = 0; i < tl.depot.length; i++) {
					td = tl.depot[i];
					if (td == undefined || td == null) {
						continue;
					}
					flag = false;
					if (Math.abs(item.series.xaxis.p2c(td.end.x) - xy[0]) < 4 && Math.abs(item.series.yaxis.p2c(td.end.y) - xy[1]) < 4) {
						flag = true;
					}
					if (Math.abs(item.series.xaxis.p2c(td.start.x) - xy[0]) < 4 && Math.abs(item.series.yaxis.p2c(td.start.y) - xy[1]) < 4) {
						tl.end = false;
						flag = true;
					}
                    
					if (flag) {
                        if (td.start.x == td.end.x && td.start.y == td.end.y) {
                            tl.end = true;
                        }
                        dw.start = "trendline";
                        target.css("cursor", "nw-resize");
                        tl.currentIndex = i;
                        istl = true;
                        break;
                    }
                    else 
                        if (tl.func.dragReDraw) {//moving
                            var r = this._mouseOverLine(xy, {
                                spos: [item.series.xaxis.p2c(td.start.x), item.series.yaxis.p2c(td.start.y)],
                                epos: [item.series.xaxis.p2c(td.end.x), item.series.yaxis.p2c(td.end.y)]
                            });
                            if (r) {
                                this._storeBeforeMoving(tl, td, item, xy[1], target);
                                dw.start = "trendline";
                                tl.currentIndex = i;
                                istl = true;
                                break;
                            }
                        }
				}
				if(istl){
					dw.operationType="drag-trendline";
				}
			}
			if ((rt.func.clickRedraw || rt.func.dragReDraw) && rt.count > 0 && !istl) {
				var rti = null, inaera = false;
				var tpos = [], t = {
					spos: [],
					epos: []
				};
				for (var i = 0; i < rt.depot.length; i++) {
					rti = rt.depot[i];
					if (rti == undefined) {
						continue;
					}
					rti.haveDrag = false;
					rti.oldStart = $.extend(true,{},rti.start);
					rti.oldEnd = $.extend(true,{},rti.end);
					t.spos = [axes.xaxis.p2c(rti.start.x), axes.yaxis.p2c(rti.start.y)];
					t.epos = [axes.xaxis.p2c(rti.end.x), axes.yaxis.p2c(rti.end.y)];
					if (t != undefined) {
						if (canvasX > (t.spos[0] - 2) && canvasX < (t.epos[0] + 2) && canvasY > (t.spos[1] - 2) && canvasY < (t.epos[1] + 2)) {
							if (rt.func.clickRedraw) {
								inaera = true;
								if (Math.abs(t.spos[0] - canvasX) < 3 && Math.abs(t.spos[1] - canvasY) < 3) {
									target.css("cursor", "nw-resize");//1
									rti.start.x = rti.end.x;
									rti.start.y = rti.end.y;
								} else if (Math.abs(t.epos[0] - canvasX) < 3 && Math.abs(t.epos[1] - canvasY) < 3) {
									target.css("cursor", "nw-resize");//3
								} else if (Math.abs(t.epos[0] - canvasX) < 3 && Math.abs(t.spos[1] - canvasY) < 3) {
									target.css("cursor", "ne-resize");//2
									rti.start.x = rti.start.x;
									rti.start.y = rti.end.y;
								} else if (Math.abs(t.spos[0] - canvasX) < 3 && Math.abs(t.epos[1] - canvasY) < 3) {
									target.css("cursor", "ne-resize");//4
									rti.start.x = rti.end.x;
									rti.start.y = rti.start.y;
								} else {
									inaera = false;
								}
							}
							
							if (rt.func.dragReDraw && !inaera) {
								if (rt.func.dragReDraw) {
									inaera = true;
									this._storeBeforeMoving(rt,rti,item,xy[1],target);
								} else {
									inaera = false;
								}
							}
						}
						if (inaera) {
							rt.currentIndex = i;
							dw.start = "rectangle";
							istl = true;
							dw.operationType="drag-rectangle";
							break;
						}
					}
				}
			}
            if ((ell.func.clickRedraw || ell.func.dragReDraw) && ell.count > 0 && !istl) {
                var elli = null, inaera = false;
                var tpos = [], t = {
                    spos: [],
                    epos: []
                };
                for (var i = 0; i < ell.depot.length; i++) {
                    elli = ell.depot[i];
                    if (elli == undefined) {
                        continue;
                    }
                    t.spos = [axes.xaxis.p2c(elli.start.x), axes.yaxis.p2c(elli.start.y)];
                    t.epos = [axes.xaxis.p2c(elli.end.x), axes.yaxis.p2c(elli.end.y)];
                    if (t != undefined) {
						if (canvasX > (t.spos[0] - 2) && canvasX < (t.epos[0] + 2) && canvasY > (t.spos[1] - 2) && canvasY < (t.epos[1] + 2)) {
                            if (ell.func.clickRedraw) {
								inaera = true;
								if (Math.abs(t.spos[0] - canvasX) < 3 && Math.abs(t.spos[1] - canvasY) < 3) {
									target.css("cursor", "nw-resize");//1
									var t = $.extend(true,{},elli.start);
									elli.start.x = elli.end.x;
									elli.start.y = elli.end.y;
									elli.end.x=t.x;
									elli.end.y=t.y;
								} else if (Math.abs(t.epos[0] - canvasX) < 3 && Math.abs(t.epos[1] - canvasY) < 3) {
									target.css("cursor", "nw-resize");//3
								} else {
									inaera = false;
								}
							}
							if (ell.func.dragReDraw&& !inaera) {
								inaera=true;
								this._storeBeforeMoving(ell,elli,item,xy[1],target);
                         	}
                             if (inaera) {
                                 ell.currentIndex = i;
								 ell.resizeHandler = true;
								 dw.operationType="drag-ellipse";
                                 dw.start = "ellipse";
                                 istl = true;
                                 break;
                             }
                        }
                    }
                }
				if(!istl){
					 ell.resizeHandler = false;
				}
            }
			if ((fi.func.clickRedraw||fi.func.movingDraw) && fi.count > 0&& !istl) {
				var td = null;
				var flag = false;
				for (var i = 0; i < fi.depot.length; i++) {
					td = fi.depot[i];
					fi.end = true;
					if (td == undefined || td == null) {
						continue;
					}
					flag = false;
					if (Math.abs(item.series.xaxis.p2c(td.end.x) - xy[0]) < 4 && Math.abs(item.series.yaxis.p2c(td.end.y) - xy[1]) < 4) {
						flag = true;
					}
					if (Math.abs(item.series.xaxis.p2c(td.start.x) - xy[0]) < 4 && Math.abs(item.series.yaxis.p2c(td.start.y) - xy[1]) < 4) {
						flag = true;
						fi.end = false;
					}
					if (flag) {
						if(td.start.x==td.end.x&&td.start.y==td.end.y){
							tl.end = true;
						}
						target.css("cursor", "se-resize");
						dw.start = "fibonacci";
						fi.currentIndex = i;
						istl = true;
						break;
					}else if(fi.func.movingDraw){
						var x1=item.series.xaxis.p2c(td.start.x),y1 = item.series.yaxis.p2c(td.start.y);
						var x2=item.series.xaxis.p2c(td.end.x);y2=item.series.yaxis.p2c(td.end.y);
						var height = y2-y1;
						var c1 = {spos:[x1,y1],epos:[x2,y2]},c2 = {spos:[x1,y2],epos:[x2,y2]},c3 = {spos:[x1,y1],epos:[x2,y1]},c4 = {spos:[x1,y2-height*0.618],epos:[x2,y2-height*0.618]},c5 = {spos:[x1,y2-height*0.5],epos:[x2,y2-height*0.5]},c6 = {spos:[x1,y2-height*0.382],epos:[x2,y2-height*0.382]};
						//r =this._mouseOverLine(xy,c);
						if(this._mouseOverLine(xy,c1)||this._mouseOverLine(xy,c2)||this._mouseOverLine(xy,c3)||this._mouseOverLine(xy,c4)||this._mouseOverLine(xy,c5)||this._mouseOverLine(xy,c6)){
							target.css("cursor", "move");
							dw.start = "fibonacci";
							fi.currentIndex = i;
							fi.move.moving = true;
							fi.move.cp = [item.datapoint[0], item.series.yaxis.c2p(xy[1])];
							fi.move.sp = [td.start.x, td.start.y];
							fi.move.ep = [td.end.x, td.end.y];
							istl = true;
							break;
						}
					}
				}
			}
			if (ll.func.dragReDraw && ll.count > 0 && !istl) {
                var lli = null, inaera = false;
                var tpos = [], t = {
                    spos: [],
                    epos: []
                };
                for (var i = 0; i < ll.depot.length; i++) {
                    lli = ll.depot[i];
                    if (lli == undefined) {
                        continue;
                    }
                    var tyy = axes.yaxis.p2c(lli.start.y);
					if(Math.abs(canvasY-tyy)<2){
						dw.start = "levelline";
						ll.currentIndex = i;
						istl = true;
						ll.move.moving = true;
                        target.css("cursor", "move");
                        break;
					}
                }
            }
			 if (nt.func.moving && nt.count > 0 && !istl) {
                var elli = null, inaera = false;
                var tpos = [], t = {
                    spos: [],
                    epos: []
                };
            }
			if(istl){
				e.preventDefault();
				e.stopPropagation();
			}
		}
		if(dw.abbDom !=null){
			dw.abbDom.remove();
		}
	},
	_levellineRelateMovingDraw:function(obj,canvasX,canvasY,item,plot){
		if (obj.func.movingDraw) {
            var depot = obj.depot[obj.currentIndex];
            if (canvasX > -plot.left &&item!=null) {
                    depot.start = {
                        x: item.datapoint[0],
                        y: item.series.yaxis.c2p(canvasY)
                    };
               // }
            }
        }
	},
	_setTrendlineText:function(depot){
        var v = (depot.end.y-depot.start.y)/depot.start.y*100;
		var ra = QS_NumFmt.Fmt2(v>0?v:v*(-1),true," ",2);
		var text =v>=0?("+"+ra+QSLGTXT.percent):("-"+ra+QSLGTXT.percent);
		depot.opDom.find(".rtq-trendline-hinttext").html(text);
	},
	mouseMove:function(item,canvasX,canvasY,plot,target){
		var dw = this.drawing;
		var tl = dw.trendline,rt = dw.rectangle,nt = dw.note,ell = dw.ellipse,fi = dw.fibonacci;
		if(dw.start !=""){
			if(dw.start == "trendline"){
                if (tl.move.moving) {
                    this._rectangleRelateMovingDraw(tl,canvasX,canvasY,item,plot);
                }
                else {
                    if (tl.func.movingDraw && item != null) {
                        var depot = tl.depot[tl.currentIndex];
                        if (tl.end) {
                            depot.end = {
                                x: item.datapoint[0],
                                y: item.series.yaxis.c2p(canvasY)
                            };
                        }
                        else {
                            depot.start = {
                                x: item.datapoint[0],
                                y: item.series.yaxis.c2p(canvasY)
                            };
                        }
                        if (depot.opDom.find(".changeHintDiv:visible").length == 0) {
                            depot.opDom.show(100);
                            this._setTrendlineText(depot);
                        }
                    }
                }
			}else if(dw.start =="rectangle"){
				this._rectangleRelateMovingDraw(rt,canvasX,canvasY,item,plot);
			}else if(dw.start =="ellipse"){
				this._rectangleRelateMovingDraw(ell,canvasX,canvasY,item,plot);
			}else if(dw.start=="levelline"){
				this._levellineRelateMovingDraw(ll,canvasX,canvasY,item,plot);
			}else if(dw.start=="fibonacci"){
				this._fibonacciRelateMovingDraw(fi,canvasX,canvasY,item,plot);
			}
		}
		if(dw.start ==""){
			if(dw.abbDom !=undefined && dw.abbDom !=null){
				dw.abbDom.remove();
			}
            if (dw.type.trendline) {
                dw.abbDom = tl.abbDom;
                target.append(dw.abbDom);
            } else if (dw.type.rectangle) {
                dw.abbDom = rt.abbDom;
                target.append(dw.abbDom);
            }else if (dw.type.note) {
                dw.abbDom = nt.abbDom;
                target.append(dw.abbDom);
            }else if (dw.type.ellipse) {
                dw.abbDom = ell.abbDom;
                target.append(dw.abbDom);
            }else if (dw.type.fibonacci) {
                dw.abbDom = fi.abbDom;
                target.append(dw.abbDom);
            }
		}
		if(dw.abbDom !=null && dw.start ==""){
			dw.abbDom.css({position:"absolute",left:(canvasX+ plot.left+10),top:canvasY-5})
		}
	},
	dbClick:function(e,xy,item){
		if(this.drawing.trendline.func.dbClickDeleteLine){
			var tl = this.drawing.trendline.depot;
			var td = 0,c,r;
			for(var i = 0;i<tl.length;i++){
				td = tl[i];
				if(td ==undefined){
					continue;
				}
				
				c = {spos:[item.series.xaxis.p2c(td.start.x),item.series.yaxis.p2c(td.start.y)],epos:[item.series.xaxis.p2c(td.end.x),item.series.yaxis.p2c(td.end.y)]};
				r =this._mouseOverLine(xy,c);
				if(r){
					this._clearTrendLine(this,i);
					break;
				}
			}
		}
		if(this.drawing.levelline.func.dbClickDeleteLine){
			var tl = this.drawing.levelline.depot;
			var td = 0,c,r;
			for(var i = 0;i<tl.length;i++){
				td = tl[i];
				if(td ==undefined){
					continue;
				}
                var tyy = item.series.yaxis.p2c(td.start.y);
                if (Math.abs(xy[1] - tyy) < 2) {
                    this._clearLevelline(this,i);
					break;
                }
			}
		}
	},
	_autoDrawThmbunail:function(obj){
		var d = this.series[0].data,len = d.length;
		var sxi = Math.floor(len*5/11),exi = Math.ceil(len*6/11);//sxi = startXIndex,exi = endXIndex
		var dy =this.series[0].yaxis.datamax-this.series[0].yaxis.datamin;
		var syi = this.series[0].yaxis.datamin+dy*5/8,eyi =this.series[0].yaxis.datamin+dy*3/8;
		obj.depot[obj.currentIndex].start={dataIndex:11,x:sxi,y:syi};
		obj.depot[obj.currentIndex].end={dataIndex:11,x:exi,y:eyi};
	},
	mouseUp:function(e,xy,item,offset,plot,plotHeight,axes,target){
		var canvasX=xy[0],canvasY=xy[1];
		var dw = this.drawing;
		var dw = this.drawing,tl = dw.trendline,rt = dw.rectangle,ell = dw.ellipse;nt = dw.note,ll = dw.levelline,fi=dw.fibonacci;
		var canvasX=xy[0],canvasY=xy[1];
		var dtype = "";
		if(dw.start !=""){
	        if (item) {
	            item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plot.left);
	            item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plot.top);                   
	        }
			if(item !=null){
				dw.count = item.series.data.length;
				if(item.series.data.length>0){
					dw.startend = item.series.data[0][item.series.data[0].length-1]+"_"+item.series.data[item.series.data.length-1][item.series.data[item.series.data.length-1].length-1];
				}
			}
			dw.operationType="";
			if(dw.start =="trendline"){
//				if (item != null&&canvasY>=0&&canvasY<(plotHeight+plot.bottom)) {
//					if (tl.end) {
//						tl.depot[tl.currentIndex].end = {
//							x: item.datapoint[0],
//							y: item.series.yaxis.c2p(canvasY)
//						};
//					} else {
//						tl.depot[tl.currentIndex].start = {
//							x: item.datapoint[0],
//							y: item.series.yaxis.c2p(canvasY)
//						};
//					}
//					var currentT = tl.depot[tl.currentIndex];
//					var ra = ((currentT.end.y-currentT.start.y)/currentT.start.y*100).toFixed(2);
//					var text =ra>=0?("+"+ra+"%"):("-"+(-ra)+"%");
//					currentT.opDom.find(".rtq-trendline-hinttext").html(text);
//				}
				dw.type.trendline = false;
				tl.depot[tl.currentIndex].opDom.show(100);
				tl.end = true;
				dtype="trendline";
				if(tl.depot[tl.currentIndex].start.x&&(typeof(tl.depot[tl.currentIndex].end)=="undefined"||!tl.depot[tl.currentIndex].end.x)){ //== tl.depot[tl.currentIndex].end.x&&tl.depot[tl.currentIndex].start.y==tl.depot[tl.currentIndex].end.y){
					this._autoDrawThmbunail(tl);
					tl.depot[tl.currentIndex].opDom.show(100);
					this._setTrendlineText(tl.depot[tl.currentIndex]);
					
				}
				dw.start="";
			}else if(dw.start =="rectangle"){
				this._getMouseUpPos(rt,item);
				if(!rt.depot[rt.currentIndex].haveDrag){
					rt.depot[rt.currentIndex].start = rt.depot[rt.currentIndex].oldStart;
					rt.depot[rt.currentIndex].end = rt.depot[rt.currentIndex].oldEnd;
				}
				if(!rt.depot[rt.currentIndex].start||typeof(rt.depot[rt.currentIndex].start.x)=="undefined"){
					this._autoDrawThmbunail(rt);
				}
					var tpos=this._calculateRectanglePos(rt.depot[rt.currentIndex],axes);
					var tdom = this._drawingAddDom(rt,"rectangle","closeRectangle",target);
					tdom.css({left:tpos.x+tpos.w+plot.left-15,top:tpos.y+5});
					rt.depot[rt.currentIndex].opDom = tdom;
				
				dw.type.rectangle = false;
				dtype="rectangle";
			}else if(dw.start=="ellipse"){
				this._getMouseUpPos(ell,item);
                if (!ell.depot[ell.currentIndex].start || typeof(ell.depot[ell.currentIndex].start.x) == "undefined") {
                    this._autoDrawThmbunail(ell);
                } 
                    var tpos = this._calculateRectanglePos(ell.depot[ell.currentIndex], axes);
                    var tdom = this._drawingAddDom(ell, "ellipse", "closeEllipse", target);
                    this._drawingSetClosePos(tdom, tpos.x + tpos.w + plot.left - 15, tpos.y + tpos.h / 2 - 3);
                    ell.depot[ell.currentIndex].opDom = tdom;
              
				dw.type.ellipse = false;
				dtype="ellipse";
			}else if(dw.start=="note"){
				dw.type.note = false;
				dtype="note";
			}else if(dw.start=="levelline"){
				ll.currentIndex = -1;
				dw.type.levelline = false;
//				if (item != null&&canvasY>=0&&canvasY<(plotHeight+plot.bottom)) {
//					tl.depot[tl.currentIndex].start = {
//						x: item.datapoint[0],
//						y: item.series.yaxis.c2p(canvasY),
//						dataIndex:this._transDataIndex(item,item.dataIndex)
//					};
//					var currentT = tl.depot[tl.currentIndex];
//					var ra = ((currentT.end.y-currentT.start.y)/currentT.start.y*100).toFixed(2);
//					var text =ra>=0?("+"+ra+"%"):("-"+(-ra)+"%");
//					currentT.opDom.find(".rtq-trendline-hinttext").html(text);
//				}
//				dw.type.trendline = false;
//				tl.depot[tl.currentIndex].opDom.show(100);
//				tl.end = true;
				dtype="levelline";
				dw.start="";
			}else if(dw.start=="fibonacci"){
				dw.type.fibonacci = false;
				dtype="fibonacci";
				fi.end = true;
				fi.move.moving = false;
				if(fi.depot[fi.currentIndex].opDom.length<11){
					var tdom = this._drawingAddDom(fi,"fibonacci","closeFibonacci",target);
					fi.depot[fi.currentIndex].opDom.push(tdom);
				}
				if(typeof(fi.depot[fi.currentIndex].end.x)=="undefined"){
					this._autoDrawThmbunail(fi);
//					var d = this.series[0].data,len = d.length;
//					var sxi = Math.floor(len*5/11),exi = Math.ceil(len*6/11);//sxi = startXIndex,exi = endXIndex
//					var dy =this.series[0].yaxis.datamax-this.series[0].yaxis.datamin;
//					var syi = this.series[0].yaxis.datamin+dy*5/8,eyi =this.series[0].yaxis.datamin+dy*3/8;
//					fi.depot[fi.currentIndex].start={dataIndex:11,x:sxi,y:syi};
//					fi.depot[fi.currentIndex].end={dataIndex:11,x:exi,y:eyi};
//					target.find(".chart-fibonacci-index-"+fi.currentIndex).remove();
//					target.find(".rtq-fibonacci-close-"+fi.currentIndex+"[index='"+fi.currentIndex+"']").unbind(".fibonacci").remove();
//					fi.count--;
				}
			}
			dw.start ="";
		}
		dw.type.note = false;
		this._drawingClearAllCloseDom(dw,rt,"rectangle","closeRectangle",target);
		this._drawingClearAllCloseDom(dw,ell,"ellipse","closeEllipse",target);
		//this._drawingClearAllCloseDom(dw,ell,"levelline","closeEllipse",target);
		if(tl.move.moving){
            tl.move.moving = false;
        }
        if(rt.move.moving){
			rt.move.moving = false;
		}
		if(ell.move.moving){
			ell.move.moving = false;
		}
		if(dw.abbDom !=null){
			dw.abbDom.remove();
		}
		return dtype;
	},
	_drawingSetClosePos:function(tdom,left,top){
		if(tdom!=null){
			tdom.css({left:left,top:top});
		}
	},
	_drawingClearAllCloseDom:function(dw,ell,type,close,target){
		if(!dw.type[type]&&ell.count<=0&&ell.depot.length>0){
			var arr = target.find(".rtq-"+type+"-close");
			for(var i = 0;i<arr.length;i++){
				$(arr[i]).unbind("."+close).remove();
			}
			ell.depot.length=0;
		}
	},
	_clearTrendLine:function(self,i){
		var td = self.drawing.trendline.depot ;
		$(td[i].opDom).find("a").unbind(".tline");
		td[i].opDom.remove();
		delete self.drawing.trendline.depot[i];
		self.drawing.trendline.count--;
	} ,
	_clearLevelline:function(self,i){
		var td = self.drawing.levelline.depot ;
		$(td[i].opDom).find("a").unbind(".levelline");
		td[i].opDom.remove();
		delete self.drawing.levelline.depot[i];
		self.drawing.levelline.count--;
	} ,
	_drawingAddDom:function(ell,type,click,target){
        var tdom = target.find(".rtq-" + type + "-close[index='" + ell.currentIndex + "']");
        if (tdom.length == 0) {
            tdom = $("<div index='" + ell.currentIndex + "' class='rtq-" + type + "-close'>&nbsp;</div>");
            tdom.appendTo(target);
            var self = this;
            tdom.bind("click." + click, function(e){
                var obj = $(e.target).parent();
                if (obj.hasClass("rtq-" + type + "-close")) {
                    var ti = parseInt(obj.attr("index"));
                    obj.remove();
					if(type=="fibonacci"){
						target.find(".chart-fibonacci-index-"+ti).remove();
					}
                    delete ell.depot[ti];
                    ell.count--;
                }
            })
			
			if(typeof(__isMobile)!="undefined"&&__isMobile===true){
                tdom.html("<img src='"+QS_ImgServer+(QS_Theme=="black"?"theme/black/btn_close.gif":"img/btn_close.gif")+"'/>");
			}else{
				tdom.hover(function(){
	                var obj = $(this);
	                if (obj.offset().top > target.offset().top) {
	                    tdom.html("<img src='"+QS_ImgServer+(QS_Theme=="black"?"theme/black/btn_close.gif":"img/btn_close.gif")+"'/>");
	                }
	            }, function(){
	                tdom.html("&nbsp;");
	            });
			}
            
        }
        return tdom;
	},
	_getMouseUpPos:function(ell,item){
        if (!ell.func.movingDraw) {
            ell.depot[ell.currentIndex].end = {
                x: item.datapoint[0],
                y: item.series.yaxis.c2p(xy[1])
            }
        }
        if (ell.func.dragReDraw && !ell.func.movingDraw) {
            var depot = ell.depot[ell.currentIndex];
            if (ell.move.moving) {
                var pyx = item.datapoint[0] - ell.move.cp[0], pyy = item.series.yaxis.c2p(canvasY) - ell.move.cp[1];
                depot.start = {
                    x: ell.move.sp[0] + pyx,
                    y: ell.move.sp[1] + pyy
                };
                depot.end = {
                    x: ell.move.ep[0] + pyx,
                    y: ell.move.ep[1] + pyy
                };
            }
        }
        ell.depot[ell.currentIndex] = this._calculateRectangleSE(ell.depot[ell.currentIndex]);
	},	_mouseOverLine:function(xy,c){
		//c is currentLine ,xy is current pos
		var minx = Math.min(c.spos[0],c.epos[0]);
		var maxx = Math.max(c.spos[0],c.epos[0]);
		var miny = Math.min(c.spos[1],c.epos[1]);
		var maxy = Math.max(c.spos[1],c.epos[1]);
		if(!(Math.round(xy[0])>minx-2&&Math.round(xy[0])<maxx+2&&Math.round(xy[1])>miny-2&&Math.round(xy[1])<maxy+2)){
			return false;
		}
		k =(c.epos[1]-c.spos[1])/(c.epos[0]-c.spos[0]); 
		b =c.spos[1]-k*c.spos[0];
		var r = k*xy[0]+b;
		if(Math.abs(r-xy[1])>3){
			return false;
		}
		return true;
	}, 
 	_getResultItem: function(i,j) {
        return { datapoint: this.series[i].data[j],dataIndex: j,series: this.series[i],seriesIndex: i};
    },
	_transDataIndex:function(item,i){
		if(item.series.data[i]==undefined){
			return i;
		}
		var ix = item.series.data[i][item.series.data[0].length-1];
		return ix;
	},
    _trendlineRelateMovingDraw: function(obj,canvasX,canvasY,item,plot){
        if (obj.func.movingDraw) {
            var depot = obj.depot[obj.currentIndex];
            depot.haveDrag = true;
            if (canvasX > -plot.left &&item!=null) {
                if (obj.move.moving) {
                    var pyx = item.datapoint[0] - obj.move.cp[0], pyy = item.series.yaxis.c2p(canvasY) - obj.move.cp[1];
                    depot.start = {
                        x: obj.move.sp[0] + pyx,
                        y: obj.move.sp[1] + pyy
                    };
                    depot.end = {
                        x: obj.move.ep[0] + pyx,
                        y: obj.move.ep[1] + pyy
                    };
                } else {
                    if (obj.end) {
                        depot.end = {
                            x: item.datapoint[0],
                            y: item.series.yaxis.c2p(canvasY)
                        };
                    }
                    else {
                        depot.start = {
                            x: item.datapoint[0],
                            y: item.series.yaxis.c2p(canvasY)
                        };
                    }
                }
            }
        }
    },
	_rectangleRelateMovingDraw: function(obj,canvasX,canvasY,item,plot){
        if (obj.func.movingDraw) {
            var depot = obj.depot[obj.currentIndex];
			depot.haveDrag = true;
            if (canvasX > -plot.left &&item!=null) {
                if (obj.move.moving) {
                    var pyx = item.datapoint[0] - obj.move.cp[0], pyy = item.series.yaxis.c2p(canvasY) - obj.move.cp[1];
                    depot.start = {
                        x: obj.move.sp[0] + pyx,
                        y: obj.move.sp[1] + pyy
                    };
                    depot.end = {
                        x: obj.move.ep[0] + pyx,
                        y: obj.move.ep[1] + pyy
                    };
                } else {
                    depot.end = {
                        x: item.datapoint[0],
                        y: item.series.yaxis.c2p(canvasY)
                    };
                }
            }
        }
	},
	_fibonacciRelateMovingDraw: function(obj,canvasX,canvasY,item,plot){
        if (obj.func.movingDraw) {
            var depot = obj.depot[obj.currentIndex];
            if (canvasX > -plot.left &&item!=null) {
                if (obj.move.moving) {
                    this._translationPos(item,obj,canvasY,depot);
                } else {
					if(obj.end){
						depot.end = {x:item.datapoint[0],y:item.series.yaxis.c2p(canvasY)};
					}else{
						depot.start = {x:item.datapoint[0],y:item.series.yaxis.c2p(canvasY)};
					}
                }
            }
        }
	},
	_translationPos:function(item,obj,canvasY,depot){
		var pyx = item.datapoint[0] - obj.move.cp[0], pyy = item.series.yaxis.c2p(canvasY) - obj.move.cp[1];
        depot.start = {
            x: obj.move.sp[0] + pyx,
            y: obj.move.sp[1] + pyy
        };
        depot.end = {
            x: obj.move.ep[0] + pyx,
            y: obj.move.ep[1] + pyy
        };
	},
	changeClosePos:function(axes,plot){
        this.changeRectangleClosePos(axes,plot);
        this.changeEllipseClosePos(axes,plot);
		 this.changeNoteStartPos(axes,plot);
	},
	clearDrawing:function(target){
		this.removeAllTrendLine();
		//this.removeAllRectangle();
		this._clearRectangle(target);
		this._clearEllipse(target);
		this._clearNotes();
		this._clearAllLevelline();
		this._clearFibonacci(target);
	},
	removeAllTrendLine:function(){
		var tl = this.drawing.trendline,temp=null;
		for(var i = 0;i<tl.depot.length;i++){
			temp = tl.depot[i];
			if(!temp){
				continue;
			}
			$(temp.opDom).find("a").unbind(".tline");
			temp.opDom.remove();
			delete tl.depot[i];
		}
		tl.count =0;
		tl.depot.length = 0;
		
	},	
	_clearRectangle:function(target){
		var dw = this.drawing,rt = dw.rectangle;
        if (!dw.type.rectangle && rt.depot.length > 0) {
            for (var i = 0; i < rt.depot.length; i++) {
                if (typeof(rt.depot[i]) != "undefined"&& rt.depot[i].opDom!=null) {
                    rt.depot[i].opDom.find("a").unbind(".closeRectangle").remove();
                }
            }
        }
		rt.depot.length=0;
		var arr = target.find(".rtq-rectangle-close");
        for (var i = 0; i < arr.length; i++) {
            $(arr[i]).unbind(".closeRectangle").remove();
        }
	},
	_clearFibonacci:function(target){
		var dw = this.drawing,rt = dw.fibonacci;
		rt.depot.length=0;
		var arr = target.find(".rtq-chart-fibonacci-tip");
        for (var i = 0; i < arr.length; i++) {
            $(arr[i]).remove();
        }
		var arr = target.find(".rtq-fibonacci-close");
        for (var i = 0; i < arr.length; i++) {
            $(arr[i]).unbind(".closeFibonacci").remove();
        }
	},
	_clearEllipse:function(target){
        var dw = this.drawing, ell = dw.ellipse;
        if (!dw.type.ellipse && ell.depot.length > 0) {
            for (var i = 0; i < ell.depot.length; i++) {
                if (typeof(ell.depot[i]) != "undefined"&&ell.depot[i].opDom!=null) {
                    ell.depot[i].opDom.find("a").unbind(".closeEllipse").remove();
                }
            }
        }
        ell.depot.length = 0;
		var arr = target.find(".rtq-ellipse-close");
		for (var i = 0; i < arr.length; i++) {
            $(arr[i]).unbind(".closeEllipse").remove();
        }
	},
	_clearAllLevelline:function(){
        var dw = this.drawing, ell = dw.levelline;
        if (!dw.type.levelline && ell.depot.length > 0) {
            for (var i = 0; i < ell.depot.length; i++) {
                if (typeof(ell.depot[i]) != "undefined"&&ell.depot[i].opDom!=null) {
                    ell.depot[i].opDom.unbind(".levelline").remove();
                }
            }
        }
        ell.depot.length = 0;
	},
	_clearNotes:function(){
        var dw = this.drawing, ell = dw.note;
        if (!dw.type.note && ell.depot.length > 0) {
            for (var i = 0; i < ell.depot.length; i++) {
                if (typeof(ell.depot[i]) != "undefined"&&ell.depot[i].note!=null) {
                    ell.depot[i].note.unBindEvent();
                }
            }
        }
        ell.depot.length = 0;
	},
	changeRectangleClosePos:function(axes,plot){
		var dw = this.drawing,rt = dw.rectangle,tpos=null,temp=null;
		for(var i = 0;i<rt.depot.length;i++){
			temp = rt.depot[i];
			if(temp !=undefined && temp !=null){
				tpos=this._calculateRectanglePos(temp,axes);
				//temp.opDom.css({left:tpos.x+tpos.w+plot.left-15,top:tpos.y+5});
				this._drawingSetClosePos(temp.opDom,tpos.x+tpos.w+plot.left-15,tpos.y+5);
			}
		}
	},
	changeEllipseClosePos:function(axes,plot){
		var dw = this.drawing,ell = dw.ellipse,tpos=null,temp=null;
		for(var i = 0;i<ell.depot.length;i++){
			temp = ell.depot[i];
			if(temp !=undefined && temp !=null){
				tpos=this._calculateRectanglePos(temp,axes);
				//temp.opDom.css({left:tpos.x+tpos.w+plot.left-15,top:tpos.y+5});
				this._drawingSetClosePos(temp.opDom,tpos.x+tpos.w+plot.left-15,tpos.y+tpos.h/2-3);
			}
		}
	},
	changeNoteStartPos:function(axes,plot){
		var dw = this.drawing,ell = dw.note,tpos=null,temp=null;
		for(var i = 0;i<ell.depot.length;i++){
			temp = ell.depot[i];
			if(temp !=undefined && temp !=null){
				var x = axes.xaxis.p2c(temp.x),y=axes.yaxis.p2c(temp.y)
				temp.note.setPosition(x,y);
				//tpos=this._calculateRectanglePos(temp,axes);
				//temp.opDom.css({left:tpos.x+tpos.w+plot.left-15,top:tpos.y+5});
				//this._drawingSetClosePos(temp.opDom,tpos.x+tpos.w+plot.left-15,tpos.y+tpos.h/2-3);
			}
		}
	},
	_calculateRectangle:function(t,axes){
		var tpos =[],r = {spos:[],epos:[]};
		r.spos=[axes.xaxis.p2c(t.start.x),axes.yaxis.p2c(t.start.y)];
		r.epos=[axes.xaxis.p2c(t.end.x),axes.yaxis.p2c(t.end.y)];
		var tp={};
		if(r.spos[0]<r.epos[0]){
			if(r.epos[1]<r.spos[1]){
				tp.x=r.spos[0],tp.y=r.epos[1];
			}else{
				tp.x=r.spos[0],tp.y=r.spos[1];
			}
		}else{
			if(r.epos[1]<r.spos[1]){
				tp.x=r.epos[0],tp.y=r.epos[1];
			}else{
				tp.x=r.epos[0],tp.y=r.spos[1];
			}
		}
		tp.w=Math.abs(r.epos[0]-r.spos[0]),tp.h=Math.abs(r.epos[1]-r.spos[1]);
		return tp;
	},
	//start and end pos
	_calculateRectangleSE:function(t){
		var tp={start:{x:0,y:0},end:{x:0,y:0}};
		tp = $.extend({},tp,t);
		if(t.start.x<t.end.x){
			if(t.start.y>t.end.y){
				
			}else{
				var temp = tp.start.y;
				tp.start.y = tp.end.y;
				tp.end.y = temp;
			}
		}else{
			if(t.start.y>t.end.y){
				var temp = t.start.x;
				t.start.x = t.end.x;
				t.end.x = temp;
			}else{
				var temp1 = tp.start.x;
				var temp2 = tp.start.y;
				tp.start.x = tp.end.x;
				tp.start.y =tp.end.y;
				tp.end.x=temp1;
				tp.end.y=temp2};
		}
		return tp;
		
	},
	_calculateRectanglePos:function(t,axes){
		var tp={};
		var arr=[axes.xaxis.p2c(t.start.x),axes.yaxis.p2c(t.start.y),axes.xaxis.p2c(t.end.x),axes.yaxis.p2c(t.end.y)];
		tp.x = arr[0];
		tp.y = arr[1];
		tp.w=Math.abs(arr[0]-arr[2]),tp.h=Math.abs(arr[1]-arr[3]);
		return tp;
	}
}
mstarINoteDataManager={
	saveNote:function(cfg){},
	removeNote:function(){},
	addNote:function(){}
};
mstar.noteDataManager=function(dao){
	this.dao = $.extend(true,{secId:0,createTime:null,eventInfo:{"text":"","status":"","h":"","w":""},updateTime:null,eventId:0},dao||{});
	this.intervalObj = null; 
	this.delay = 1000;
};
mstar.noteDataManager.prototype={
	saveNote:function(obj){
		var self = this;
		clearTimeout(this.intervalObj);
        this.intervalObj = setTimeout(function(){
            self._updateNote(obj);
        }, this.delay);
	},
	_updateNote:function(obj){
		var o = $.extend(true,this.dao,obj.saveNote());
		DBHelper.updateNote(o.eventId,o.secId,$.toJSON(o.eventInfo),o.eventTime,function(data){
		})
	},
	removeNote:function(nid){
		DBHelper.removeNote(this.dao.eventId,function(){
			
		})
	},
	addNote:function(){
		var self = this;
		DBHelper.addNote(this.dao.secId,$.toJSON(this.dao.eventInfo),this.dao.eventTime,function(data){
			var o = $.evalJSON(data);
			if(o.list&&o.list.length>0){
				self.dao=$.extend(true,self.dao,o.list[0]);
			}
		});
	}
};
mstar.note=function(tar,series,dataManager){
	this.tar = tar;
	this.dom=null;
	this.series = series;
	this.nx = -1;
	this.dataManager = dataManager||mstarINoteDataManager;
	this.segmentDom={counter:null,title:null,text:null,bottom:null,middle:null,handle:null,counter:null};
	this.drag={drag:false,d:{x:0,y:0},off:{x:0,y:0}};
	this.clickPos={x:0,y:0};
	this.changeSize=false;
	this.isFocus = false;
	this.currentIndex=0;
	this.domBeforeHide={w:100,h:42,hide:false};
	this.changeSizeDelay = null;
	this.mouseoutDelay = null;
	this.mouseoverDelay = null;
	this.haveDrag = false;
	this.init();
};
mstar.note.prototype={
	init:function(){
		this.dom =$("<div class='qs-note-flot qs-note-flot-background' ><div class='qs-note-title'><a style='float:left' class='qs-note-abb'>NOTE</a><span class='qs-note-max-count'></span><a class='hideable qs-note-close' style='float:right'></a><a class='qs-note-minimize hideable' style='float:right'></a></div><div class='qs-note-middle hideable'><textarea class='qs-ui-txt'></textarea><div class='qs-note-pos-handle'>&nbsp;</div></div></div>");
		this.tar.append(this.dom);
		this.segmentDom.title = this.dom.find(".qs-note-title");
		this.segmentDom.abb = this.dom.find(".qs-note-abb");
		this.segmentDom.text = this.dom.find(".qs-ui-txt");
		this.segmentDom.middle = this.dom.find(".qs-note-middle");
		this.segmentDom.handle = this.dom.find(".qs-note-pos-handle");
		this.segmentDom.counter = this.dom.find(".qs-note-max-count");
		this._bindEvent();
	},
	addNote:function(){
		this.dataManager.addNote();
	},
	setDataManager:function(dm){
		this.dataManager=dm;
	},
	/**
	 * note's initail position
	 * @param {Object} x
	 * @param {Object} y
	 */
	setPosition:function(x,y){
		this.drag.d={x:x,y:y};
		this.dom.css({left: x + "px",top: y + "px"});
	},
	//note close
	unBindEvent:function(){
		if(typeof(this)!="undefined" && this !=null&&typeof(this.dom)!="undefined" && this.dom !=null){
			this.segmentDom.text.unbind(".qs-note-flot-self");
			this.dom.unbind(".qs-note-flot-self");
			this.dom.remove();
		}
	},
	_bindEvent:function(){
		this._textChange();//Calculate the number of characters
		this._mouseDown();//mouse click the note
		this._mouseClick();
		this._inputBlur();//note will hide,only show the text
		this._inputSelect();//note focus
		this._mouseOut();
		this._mouseOver();
	},
	_textChange:function(){
		var sf = this;
		this.segmentDom.text.unbind("keydown.qs-note-flot").bind("keydown.qs-note-flot",function(e){
			if(sf.isFocus){
                var t = this;
                setTimeout(function(){
                    var len = t.value.length;
                    if (len <= 100) {
					   sf.segmentDom.counter.show();
					   sf.segmentDom.counter.html((100-len) + " char left");
					   sf.segmentDom.abb.hide();
                    } else {
                        t.value = t.value.substr(0, 100);
                    }
					sf.dataManager.saveNote(sf);
                }, 200)
			}
		});
	},
	_mouseDown:function(){
		var sf = this;
		this.dom.unbind("mousedown.qs-note-flot-self").bind("mousedown.qs-note-flot-self",function(e){
			var tar = $(e.target);
			sf.clickPos={x:sf.dom.offset().left,y:sf.dom.offset().top};
			sf.haveDrag = false;
			if(tar.hasClass("qs-note-title")||tar.hasClass("qs-note-max-count")){ //click the title ,move
				sf.drag.drag = true;
				sf.drag.off.x = sf.dom.position().left-e.pageX;
				sf.drag.off.y = sf.dom.position().top-e.pageY;
				sf.segmentDom.title.css("cursor","move");
				sf._mouseMove();
				sf._mouseUp();
			}else if(tar.hasClass("qs-note-pos-handle")){//change note's size
				sf.changeSize = true;
				sf._mouseMove();
				sf._mouseUp();
			}
			sf.tar.find(".qs-note-flot").css("z-index",100);
			sf.dom.css("z-index",101);
			e.stopPropagation();
		})
	},
	restoreNote:function(cfg){
		if(cfg.text){
			this.segmentDom.text.html(cfg.text);
		}
		if(cfg.h&&cfg.w){
			this._domReSize(cfg.w,cfg.h);
		}
		if(cfg.status&&cfg.status=="close"){
			this._minimizeNote();
		}
	},
	removeNote:function(){
		this.dataManager.removeNote();
	},
	saveNote:function(){
		var rtn ={eventInfo:null};
		if(this.nx!=-1){
			var d = this.series.data[this.nx];
			var tempTime = d[d.length-1]>100000?d[d.length-1]:RTQ_ChartUtil.toDateFromIndex(d[d.length-1]).getTime();
			rtn["eventTime"]=tempTime;
		}
		rtn.eventInfo= {
			text: this.segmentDom.text.val(),
			status: this.dom.find(".qs-note-minimize:visible").length > 0 ? "open" : "close",
			h: this.dom.height(),
			w: this.dom.width()
		};
		return rtn;
	},
	_minimizeNote:function(){
		this.segmentDom.abb.html("NOTE");
		this.domBeforeHide = {w: this.dom.width(),h:this.dom.height(),hide:true};
		this.dom.find(".hideable").hide();
		this.dom.css({width:"auto",height:"auto"});
		this.segmentDom.abb.show();
		this.segmentDom.counter.hide();
		this.saveNote();
	},
	_mouseClick:function(){
		var sf = this;
		this.dom.unbind("click.qs-note-flot-self").bind("click.qs-note-flot-self",function(e){
			var tar = $(e.target);
			if(tar.hasClass("qs-note-minimize")){//minimize
				sf._minimizeNote();
			}else if(tar.hasClass("qs-note-abb")){ //restore
				if(sf.domBeforeHide.hide){
					sf.dom.find(".hideable").show();
					sf.segmentDom.abb.hide();
					sf.segmentDom.counter.show();
					sf.segmentDom.counter.html(100-sf.segmentDom.text.val().length + " char left");
					sf.dom.css({width:sf.domBeforeHide.w,height:sf.domBeforeHide.h});
					sf.domBeforeHide.hide=false;
					sf.saveNote();
				}
			}else if(tar.hasClass("qs-note-close")){//close
				sf.unBindEvent();
				sf.removeNote();
			}
		})
	},	
	_domReSize:function(tx,ty){
		this.dom.css({width:tx,height:ty});
		this.segmentDom.middle.css({height:ty-12});
		this.segmentDom.text.css({height:ty-12});
	},
	_mouseMove:function(){
		var sf = this;
		var tx=0,ty=0,dx=0,dy=0,nx=0;
		$(document.body).unbind("mousemove.qs-note-flot").bind("mousemove.qs-note-flot",function(e){
			var dg = sf.drag;
			if(dg.drag){
				dx=e.pageX+dg.off.x,dy=e.pageY+dg.off.y;
				if((dx)<0){dx=0;
				}
				if((dy)<0){dy=0;}
				if((dx+sf.domBeforeHide.w)>sf.tar.width()){
					dx = sf.tar.width()-sf.domBeforeHide.w;
				}
				if((dy+sf.domBeforeHide.h)>sf.tar.height()){dy = sf.tar.height()-sf.domBeforeHide.h;}
				sf.nx=Math.round(sf.series.xaxis.c2p(dx));
				sf.dom.css({left:sf.series.xaxis.p2c(sf.nx)+"px",top:dy+"px"});
				//sf.dom.css({left:dx+"px",top:dy+"px"});
			}else if(sf.changeSize){
				tx = e.pageX-sf.clickPos.x,ty=e.pageY-sf.clickPos.y;
				clearTimeout(sf.changeSizeDelay);
				setTimeout(function(){
					if(tx<90){tx=90;}
					if(tx>400){tx=400;}
					if(ty<26){ty=26;}
					if(ty>180){ty=180;}
					sf._domReSize(tx,ty);
				},10);
			}
			
		})
	},
	_inputBlur:function(){
		var sf = this;
		this.segmentDom.text.unbind("blur.qs-note-flot-self").bind("blur.qs-note-flot-self",function(){
			sf.isFocus = false;
			//sf._titleHide(sf);
			//sf.dom.find(".hideable").hide();
		})
	},
	_inputSelect:function(){
		var sf = this;
		this.segmentDom.text.unbind("focus.qs-note-flot-self").bind("focus.qs-note-flot-self",function(){
			sf.isFocus = true;
		})
	},
	_mouseUp:function(){
		var sf = this;
		$(document.body).unbind("mouseup.qs-note-flot").one("mouseup.qs-note-flot",function(e){
			if(sf.drag.drag){
				sf.drag.drag=false;
			}
			if(sf.changeSize){
				sf.changeSize= false;
				sf.haveDrag = true;
				sf.domBeforeHide.w=sf.dom.width();
				sf.domBeforeHide.h=sf.dom.height();
			}
			sf.dataManager.saveNote(sf);
			$(document.body).unbind("mousemove.qs-note-flot");
		})
	},
	_mouseOut:function(){
		var cname = "",sf=this;
		this.dom.bind("mouseleave.qs-note-flot-self",function(e){
			cname = $(e.relatedTarget).attr("class");
			if(cname.indexOf("qs-note")!=0&&!sf.haveDrag&&!sf.domBeforeHide.hide){
				clearTimeout(sf.mouseoutDelay);
				clearTimeout(sf.mouseoverDelay);
				sf.mouseoutDelay = setTimeout(function(){
                    if (sf.dom.hasClass("qs-note-flot-background")&&sf.segmentDom.text.val().length>0) {
                       	sf._titleHide(sf);
                    }
				},500);
			}
			if(sf.haveDrag){
				sf.haveDrag =false;
			}
		});
	},
    _mouseOver: function(){
        var sf = this;
        this.segmentDom.middle.unbind("mouseover.qs-note-flot-self").bind("mouseover.qs-note-flot-self", function(e){
            if (!sf.dom.hasClass("qs-note-flot-background")&&!sf.domBeforeHide.hide) {
                clearTimeout(sf.mouseoverDelay);
				clearTimeout(sf.mouseoutDelay);
                sf.mouseoverDelay = setTimeout(function(){
                   sf._titleShow(sf);
                }, 500);
            }
        });
    },
	_titleShow:function(sf){
		sf.segmentDom.title.css("visibility","visible");
		sf.segmentDom.handle.css("visibility","visible");
		sf.segmentDom.text.removeClass("qs-note-text-input-blur");
		sf.dom.css({top:sf.dom.position().top - 1,left:sf.dom.position().left - 1})
		sf.dom.addClass("qs-note-flot-background");
	},
    _titleHide: function(sf){
		sf.dom.css({top:sf.dom.position().top + 1,left:sf.dom.position().left + 1})
		sf.segmentDom.title.css("visibility","hidden");
		sf.segmentDom.text.blur();
		sf.segmentDom.text.addClass("qs-note-text-input-blur");
		sf.dom.removeClass("qs-note-flot-background");
		sf.segmentDom.handle.css("visibility","hidden");
    }
};
